package com.zyasdk.util;

import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.zyasdk.Zreceiver;
import com.zyasdk.model.NewsPortal;
import com.zyasdk.model.Products;

public class NotifiUtil {
	NotificationManager mNotificationManager;
	Context context;
	private static NotifiUtil notifiUtil;
	public static final String ZYANOTIFI_NAME = "ZYANOTIFI_NAME";
	public static final String ZYANOTIFI_TYPE = "ZYANOTIFI_TYPE";
	public static final String ZYANOTIFI_ACTION = "com.zyasdk.util.NotifiUtil";
	public static final int NOTIFI_TYPE_PRODUCT = 1;
	public static final int NOTIFI_TYPE_NEWS = 2;

	public static NotifiUtil getInstance(Context context) {
		if (notifiUtil == null) {
			notifiUtil = new NotifiUtil(context);
		}
		return notifiUtil;
	}

	private NotifiUtil(Context context) {
		this.context = context;
		mNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	/**
	 * 新闻通知栏
	 * 
	 * @param newsPortal
	 */
	public void notifiNews(NewsPortal newsPortal) {
		notifi(newsPortal.getHeader(), newsPortal.getExcerpt(),
				newsPortal.getHeader(), newsPortal, NOTIFI_TYPE_NEWS);
	}

	/**
	 * 产品通知栏
	 * 
	 * @param product
	 */
	public void notifiProduct(Products product) {
		notifi(product.getName(), "click to open application",
				product.getName(), product, NOTIFI_TYPE_PRODUCT);
	}

	@SuppressLint("InlinedApi")
	private void notifi(String title, String text, String ticker,
			Object object, int type) {
		int currHour = Integer.parseInt(DateHelper.getInstance().getCurrHour());
		int defaults = Notification.DEFAULT_ALL;
		//晚上9点到早上7点之间不进行声音提醒，只做震动以及闪灯
		if (currHour < 7 || currHour > 21) {
			defaults = Notification.DEFAULT_VIBRATE
					| Notification.DEFAULT_LIGHTS;
		}
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
				context);
		mBuilder.setContentTitle(title)
		// 设置通知栏标题
				.setContentText(text)
				// 设置通知栏显示内容
				.setContentIntent(
						getDefalutIntent(Notification.FLAG_AUTO_CANCEL, object,
								type))
				// 设置通知栏点击意图
				.setTicker(ticker)
				// 通知首次出现在通知栏，带上升动画效果的
				.setWhen(System.currentTimeMillis())
				// 通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
				.setPriority(Notification.PRIORITY_MAX)
				// 设置该通知优先级
				.setAutoCancel(true)
				// 设置这个标志当用户单击面板就可以让通知将自动取消
				.setOngoing(false)
				// ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
				.setDefaults(defaults)
				// 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
				.setSmallIcon(android.R.drawable.stat_sys_download);
				
		int notifiId = new Random().nextInt(100000);
		mNotificationManager.notify(notifiId, mBuilder.build());
	}

	/**
	 * 设置点击通知后续操作
	 * @param flags
	 * @param object
	 * @param type
	 * @return
	 */
	public PendingIntent getDefalutIntent(int flags, Object object, int type) {
		Intent intent = new Intent();
		intent.setClass(context, Zreceiver.class);
		intent.setAction(ZYANOTIFI_ACTION);
		//根据类型设置传递参数
		if (type == NOTIFI_TYPE_PRODUCT) {
			intent.putExtra(ZYANOTIFI_NAME, (Products) object);
		} else if (type == NOTIFI_TYPE_NEWS) {
			intent.putExtra(ZYANOTIFI_NAME, (NewsPortal) object);
		}
		//传递通知类型
		intent.putExtra(ZYANOTIFI_TYPE, type);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intent, flags);
		return pendingIntent;
	}
}
